
#include "IGlobal.h"
#include <BOpenGL>
#include "member_BPainter.h"
#include "member_BStyle.h"

using namespace BWE;

member_BPainter::member_BPainter(BPainter* painter)
{
	boss = painter;
	widget = 0;
	pixmap = 0;
	style = 0;
	state = 0;

	tbo = 0;
	font = 0;
	pointSmooth = false;
	pointSize = 1;
	lineStipple = 0;
	lineSmooth = false;
	lineWidth = 1;
}
member_BPainter::~member_BPainter()
{

}

const BColor* member_BPainter::findColor(StyleRole colorRole)
{
	if (colorRole > 0)
	{
		auto it = style_member(style)->colorMap.find(colorRole);
		if (it.valid())
			return &(*it);
		if (app_style && app_style != style)
		{
			it = style_member(app_style)->colorMap.find(colorRole);
			if (it.valid())
				return &(*it);
		}
	}
	return 0;
}
const BColor* member_BPainter::findColor(const char* colorName)
{
	if (colorName)
	{
		auto it = style_member(style)->namedColorMap.find(colorName);
		if (it.valid())
			return &(*it);
		if (app_style && app_style != style)
		{
			it = style_member(app_style)->namedColorMap.find(colorName);
			if (it.valid())
				return &(*it);
		}
	}
	return 0;
}
const BColor* member_BPainter::findColor(const BString& colorName)
{
	if (colorName.size())
	{
		auto it = style_member(style)->namedColorMap.find(colorName);
		if (it.valid())
			return &(*it);
		if (app_style && app_style != style)
		{
			it = style_member(app_style)->namedColorMap.find(colorName);
			if (it.valid())
				return &(*it);
		}
	}
	return 0;
}

void member_BPainter::createPoints(const BRectf& rect, float rx, float ry)
{
	float halfPI = PIf * 0.5f;
	points.reset();
	if (rx <= 0 || ry <= 0)
	{
		points.append(rect.leftTop());
		points.append(rect.leftBottom());
		points.append(rect.rightBottom());
		points.append(rect.rightTop());
		return;
	}
	//create left top points
	{
		BPointf center(rect.left() + rx, rect.top() + ry);
		if (rx == ry)
		{
			int sides = (int)bLimit(rx / 3, 3, 24);
			float sr = halfPI / sides;
			for (int i = 0; i <= sides; i++)
			{
				float angle = PIf - i * sr;
				float x = cos(angle) * rx;
				float y = sin(angle) * rx;
				points.append(center + BPointf(x, -y));
			}
		}
		else
		{
			float rx2 = rx * rx;
			float ry2 = ry * ry;
			for (float x = -rx; x < 0; x += 2)
			{
				float y = sqrt((1.0f - (x * x) / rx2) * ry2);
				points.append(center + BPointf(x, -y));
			}
			points.append(center + BPointf(0, -ry));
		}
	}
	//create right top points
	{
		BPointf center(rect.right() - rx, rect.top() + ry);
		if (rx == ry)
		{
			int sides = (int)bLimit(rx / 3, 3, 24);
			float sr = halfPI / sides;
			for (int i = 0; i <= sides; i++)
			{
				float angle = halfPI - i * sr;
				float x = cos(angle) * rx;
				float y = sin(angle) * rx;
				points.append(center + BPointf(x, -y));
			}
		}
		else
		{
			float rx2 = rx * rx;
			float ry2 = ry * ry;
			for (float x = 0; x < rx; x += 2)
			{
				float y = sqrt((1.0f - (x * x) / rx2) * ry2);
				points.append(center + BPointf(x, -y));
			}
			points.append(center + BPointf(rx, 0));
		}
	}
	//create right bottom points
	{
		BPointf center(rect.right() - rx, rect.bottom() - ry);
		if (rx == ry)
		{
			int sides = (int)bLimit(rx / 3, 3, 24);
			float sr = halfPI / sides;
			for (int i = 0; i <= sides; i++)
			{
				float angle = -i * sr;
				float x = cos(angle) * rx;
				float y = sin(angle) * rx;
				points.append(center + BPointf(x, -y));
			}
		}
		else
		{
			float rx2 = rx * rx;
			float ry2 = ry * ry;
			for (float x = rx; x > 0; x -= 2)
			{
				float y = sqrt((1 - (x * x) / rx2) * ry2);
				points.append(center + BPointf(x, y));
			}
			points.append(center + BPointf(0, ry));
		}
	}
	//create left bottom points
	{
		BPointf center(rect.left() + rx, rect.bottom() - ry);
		if (rx == ry)
		{
			int sides = (int)bLimit(rx / 3, 3, 24);
			float sr = halfPI / sides;
			for (int i = 0; i <= sides; i++)
			{
				float angle = -(halfPI + i * sr);
				float x = cos(angle) * rx;
				float y = sin(angle) * rx;
				points.append(center + BPointf(x, -y));
			}
		}
		else
		{
			float rx2 = rx * rx;
			float ry2 = ry * ry;
			for (float x = 0; x > -rx; x -= 2)
			{
				float y = sqrt((1 - (x * x) / rx2) * ry2);
				points.append(center + BPointf(x, y));
			}
			points.append(center + BPointf(-rx, 0));
		}
	}
}
